package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketAftersale;
import com.cskaoyan.bean.MarketAftersaleExample;
import com.cskaoyan.mapper.MarketAftersaleMapper;
import com.cskaoyan.model.admin.bo.AfterSaleListBO;
import com.cskaoyan.model.admin.bo.BaseParam1;
import com.cskaoyan.model.admin.vo.AfterSaleListRespVO;
import com.cskaoyan.model.admin.vo.ListEntityOfAfterSaleListRespVO;
import com.cskaoyan.uploadutil.Date2StringUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: AfterSaleServiceImpl
 * @Author: WangEnzhao
 * @Description: TODO
 * @CreateTime: 2021/12/10:11:00
 */
@Service
public class AfterSaleServiceImpl implements AfterSaleService{

    @Autowired
    MarketAftersaleMapper marketAftersaleMapper;

    /**
    * @Author: WangEnzhao
    * @Description: 售后管理模块查询
    * @CreateTime:
    * @Param:
    * @return:
    **/
    @Override
    public AfterSaleListRespVO selectAfterSaleList(BaseParam1 baseParam, AfterSaleListBO afterSaleListBO) {
        AfterSaleListRespVO afterSaleListRespVO = new AfterSaleListRespVO();

        //分页
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());

        MarketAftersaleExample marketAftersaleExample = new MarketAftersaleExample();
        MarketAftersaleExample.Criteria criteria = marketAftersaleExample.createCriteria();
        if(baseParam.getStatus() != null){
            criteria.andStatusEqualTo(baseParam.getStatus());
        }
        if(afterSaleListBO != null){
            if(afterSaleListBO.getAftersaleSn() != null && !afterSaleListBO.getAftersaleSn().equals("")){
                criteria.andAftersaleSnEqualTo(afterSaleListBO.getAftersaleSn());
            }

            if(afterSaleListBO.getOrderId() != null && !afterSaleListBO.getOrderId().equals("")){

                try{
                    criteria.andOrderIdEqualTo(Integer.parseInt(afterSaleListBO.getOrderId()));

                }catch (Exception e){
                    e.printStackTrace();
                    return afterSaleListRespVO;
                }
            }
        }
        List<MarketAftersale> marketAftersales = marketAftersaleMapper.selectByExample(marketAftersaleExample);
        //获取返回数据的total
        PageInfo pageInfo = new PageInfo(marketAftersales);
        long total = pageInfo.getTotal();
        int page = pageInfo.getPageNum();
        int limit = pageInfo.getPageSize();
        int pages = pageInfo.getPages();
        afterSaleListRespVO.setTotal((int) total);
        afterSaleListRespVO.setPage(page);
        afterSaleListRespVO.setLimit(limit);
        afterSaleListRespVO.setPages(pages);

        ArrayList<ListEntityOfAfterSaleListRespVO> listEntityOfAfterSaleListRespVOS = new ArrayList<>();

        for (MarketAftersale marketAftersale : marketAftersales) {
            ListEntityOfAfterSaleListRespVO listEntityOfAfterSaleListRespVO = new ListEntityOfAfterSaleListRespVO();
            String pictures = marketAftersale.getPictures();
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                String[] picturesArr = objectMapper.readValue(pictures, String[].class);
                listEntityOfAfterSaleListRespVO.setPictures(picturesArr);

            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            listEntityOfAfterSaleListRespVO.setAddTime(Date2StringUtil.formatWithTime(marketAftersale.getAddTime()));
            listEntityOfAfterSaleListRespVO.setAftersaleSn(marketAftersale.getAftersaleSn());
            listEntityOfAfterSaleListRespVO.setAmount(marketAftersale.getAmount());
            listEntityOfAfterSaleListRespVO.setComment(marketAftersale.getComment());
            listEntityOfAfterSaleListRespVO.setDeleted(marketAftersale.getDeleted());
            listEntityOfAfterSaleListRespVO.setHandleTime(Date2StringUtil.formatWithTime(marketAftersale.getHandleTime()));
            listEntityOfAfterSaleListRespVO.setId(marketAftersale.getId());
            listEntityOfAfterSaleListRespVO.setOrderId(marketAftersale.getOrderId());
            listEntityOfAfterSaleListRespVO.setReason(marketAftersale.getReason());
            listEntityOfAfterSaleListRespVO.setStatus(marketAftersale.getStatus());
            listEntityOfAfterSaleListRespVO.setType(marketAftersale.getType());
            listEntityOfAfterSaleListRespVO.setUpdateTime(Date2StringUtil.formatWithTime(marketAftersale.getUpdateTime()));
            listEntityOfAfterSaleListRespVO.setUserId(marketAftersale.getUserId());

            listEntityOfAfterSaleListRespVOS.add(listEntityOfAfterSaleListRespVO);
        }
        afterSaleListRespVO.setList(listEntityOfAfterSaleListRespVOS);

        return afterSaleListRespVO;
    }

    /***
    * @Author: WangEnzhao
    * @Description: 售后管理批量通过申请
    * @CreateTime: 17:37 12/1/2021
    * @Param: []
    * @return: void
    */
    @Transactional
    @Override
    public void batchReceipt(Integer[] ids) {
        MarketAftersale marketAftersale = new MarketAftersale();
        for (Integer id : ids) {
            MarketAftersaleExample marketAftersaleExample = new MarketAftersaleExample();
            marketAftersaleExample.createCriteria().andIdEqualTo(id);
            marketAftersale.setHandleTime(new Date());
            Short status = 2;
            marketAftersale.setStatus(status);
            marketAftersaleMapper.updateByExampleSelective(marketAftersale,marketAftersaleExample);
        }
    }

    /***
    * @Author: WangEnzhao
    * @Description: 售后管理批量拒绝申请
    * @CreateTime: 18:55 12/1/2021
    * @Param: [ids]
    * @return: void
    */
    @Override
    public void batchReject(Integer[] ids) {
        MarketAftersale marketAftersale = new MarketAftersale();
        for (Integer id : ids) {
            MarketAftersaleExample marketAftersaleExample = new MarketAftersaleExample();
            marketAftersaleExample.createCriteria().andIdEqualTo(id);
            marketAftersale.setHandleTime(new Date());
            Short status = 4;
            marketAftersale.setStatus(status);
            marketAftersaleMapper.updateByExampleSelective(marketAftersale,marketAftersaleExample);
        }
    }
}
